package com.lepu.app.fun.my.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.CircularImageView;
import com.lepu.app.fun.my.MyFenSiActivity;
import com.lepu.app.fun.my.bean.BeanFensi;
import com.lepu.pasm.R;

public class AdapterFensi extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private MyFenSiActivity mContext = null;
	private ArrayList<BeanFensi> mArrayList = null;
	private int mType = MyFenSiActivity.TYPE_FENSI;
	private boolean mIsSelfUserID = true;

	public AdapterFensi(MyFenSiActivity context, ArrayList<BeanFensi> arrayList,int type,boolean isSelfUserID) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		mType = type;
		mIsSelfUserID = isSelfUserID;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.my_fensi_activity_listitem, null);

			holder = new ViewHolder();

			CircularImageView iconImageView = (CircularImageView) convertView
					.findViewById(R.id.iconImageView);
			TextView titleTextView = (TextView) convertView
					.findViewById(R.id.titleTextView);
			ImageButton opeImageButton = (ImageButton) convertView
					.findViewById(R.id.opeImageButton);

			holder.iconImageView = iconImageView;
			holder.titleTextView = titleTextView;
			holder.opeImageButton = opeImageButton;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final BeanFensi item = mArrayList.get(position);
		UniversalImageLoadTool.disPlay(Setting.getHeadIconUrl(item.UserID), holder.iconImageView,
				R.drawable.app_ic_default_avatar);
		
		holder.titleTextView.setText(item.NickName);
		holder.opeImageButton.setTag(position);

		if(mIsSelfUserID)
		{
			holder.opeImageButton.setVisibility(View.VISIBLE);
			if(mType == MyFenSiActivity.TYPE_FENSI)
			{
				if(item.FollowStatus == BeanFensi.STATUS_1) //粉丝
				{
					holder.opeImageButton.setBackgroundResource(R.drawable.my_fensi_icon_add_guanzhu);
				}else if(item.FollowStatus == BeanFensi.STATUS_2)
				{
					holder.opeImageButton.setBackgroundResource(R.drawable.my_fensi_icon_xianghu_guanzhu);
				}

				holder.opeImageButton.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {

						if(item.FollowStatus == BeanFensi.STATUS_1)
						{
							mContext.addGuanZhu(item, position);
						}else if(item.FollowStatus == BeanFensi.STATUS_2){
							mContext.cancelGuanZhu(item,position);
						}
					}
				});
			}else{
				//guan zhu
				if(item.FollowStatus == BeanFensi.STATUS_1)
				{
					holder.opeImageButton.setBackgroundResource(R.drawable.my_fensi_icon_finish_guanzhu);
				}else if(item.FollowStatus == BeanFensi.STATUS_2)
				{
					holder.opeImageButton.setBackgroundResource(R.drawable.my_fensi_icon_xianghu_guanzhu);
				}

				holder.opeImageButton.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {

						if(item.FollowStatus == BeanFensi.STATUS_1 ||
								item.FollowStatus == BeanFensi.STATUS_2)
						{
							mContext.cancelGuanZhu(item,position);
						}else if(item.FollowStatus == BeanFensi.STATUS_3){
							mContext.addGuanZhu(item,position);
						}
					}
				});
			}
		}else{
			holder.opeImageButton.setVisibility(View.GONE);
		}


		holder.iconImageView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//点击头像
				LoginUtil.jumpUserInfo((BaseFragmentActivity)mContext, item.UserID);
			}
		});

		return convertView;
	}

	private static class ViewHolder {
		CircularImageView iconImageView;
		TextView titleTextView;
		ImageButton opeImageButton;
	}
}
